package com.mes.web.controller.lc;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mes.entity.lc.MaterialReturn;
import com.mes.entity.lc.ProductReturn;
import com.mes.entity.yzl.ReturnOrder;
import com.mes.entity.yzlExtend.ExtendProContactOrder;
import com.mes.service.lc.ProductReturnService;
import com.mes.utils.ResponseUtil;
import com.mes.utils.StringUtil;

/**
 * 产品退货Controller 
 * @ClassName:ProductReturnController
 * @Description:
 * @Author LC
 * @Date 2018年2月1日下午3:00:33
 * @version 1.0
 */
@Controller
@RequestMapping("/prc")
public class ProductReturnController {

	@Autowired
	ProductReturnService productReturnService;//产品退货
	
	@RequestMapping("/selectfyprc")
	public @ResponseBody void selectfyprc(
			ProductReturn productReturn,HttpServletResponse response,
			@RequestParam(value = "limit", required = false) String limit,
			@RequestParam(value = "offset", required = false) String offset)
			throws Exception {
		
			productReturn.setPage(Integer.parseInt(offset) / Integer.parseInt(limit));
			
			// 每页条数
			productReturn.setPageSize(Integer.parseInt(limit));
	
			if (StringUtil.isEmpty(productReturn.getReodd())) {
				productReturn.setReodd(null);
			}//退货单号
			if (StringUtil.isEmpty(productReturn.getRepici())) {
				productReturn.setRepici(null);
			}//批次
	
			// 物料
			List<ProductReturn> cslist = productReturnService.selectfyprc(productReturn);
			int a = productReturnService.selectfyprccont(productReturn);
			JsonConfig config = new JsonConfig();
			JSONObject jsonObject = new JSONObject();
			JSONArray jsonArray = JSONArray.fromObject(cslist, config);
			jsonObject.put("rows", jsonArray);
			jsonObject.put("total", a);
			ResponseUtil.write(response, jsonObject);
	}
	
	
	/**
	 * PDA接口      查询所有未退货且审批通过的产品退货单
	 */
	@RequestMapping("/selectprcall")
	@ResponseBody
	public Map<String,Object> selectprcall(HttpServletResponse response, HttpServletRequest request) throws Exception{
		List<ReturnOrder> list = productReturnService.selectprcall();
	    Map<String,Object> map = new HashMap<String,Object>();
	    map.put("success", "1");
		map.put("data",list);
		return map;
	}
	
	
	
	/**
	 * PDA 接口，拿到一个退货单id，然后给出对应的具体信息
	 * 
	 */
	@RequestMapping("/selectpcoall")
	public void selectpcoall(Long ids, HttpServletResponse response, HttpServletRequest request)
			throws Exception {

		//根据退货单id查询对应的具体信息
		List<ExtendProContactOrder> list = productReturnService.selectpcoall(ids);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("success", "1");
		jsonObject.put("data", list);
		ResponseUtil.write(response, jsonObject);
	}
	
	
	/**
	 * PDA  开始退货
	 * @param ids 退货单主键
	 * @param suprname 客户
	 * @param number 退货单编号
	 * @param response
	 * @param request
	 * @throws Exception
	 */
	@RequestMapping("/saveprc")
	public void savemck(Long ids,String suprname,String number,HttpServletResponse response, HttpServletRequest request)
			throws Exception {
		
		//根据收货单id查询对应的具体信息
		List<ExtendProContactOrder> list = productReturnService.selectpcoall(ids);
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd");
		String nowtime = dateFormat.format(now);

		int a = 0 ;
		for(ExtendProContactOrder  pgk : list){
			 a = productReturnService.saveprc(pgk,suprname,number,nowtime);
		}
		
		//修改资材退货单状态
		if(a > 0){
			productReturnService.updatereturnorder(ids);
		}
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("success", "1");
		jsonObject.put("data",a);
		ResponseUtil.write(response, jsonObject);
	}	
	
}
